package com.yhb.controller.admin;

import com.yhb.controller.form.LoginForm;
import com.yhb.controller.form.SimpleVerifyCodeForm;
import com.yhb.controller.vo.AdminAuthVo;
import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.controller.vo.AuthVo;
import com.yhb.controller.vo.SimpleVerifyCodeTokenVo;
import com.yhb.model.User;
import com.yhb.service.UserService;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.QueryParam;

@RestController
@RequestMapping("/admin/user")

public class AdminUserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/getCode")
    public Response<SimpleVerifyCodeTokenVo> getLoginCode(@RequestBody LoginForm form) {
        return Response.ok(new SimpleVerifyCodeTokenVo(userService.adminGetVerifyCode(form.getMobile()), form.getMobile()));
    }


    @PostMapping("/verify")
    public Response<AdminAuthVo> getLoginCode(@RequestBody SimpleVerifyCodeForm form) {
        return Response.ok(userService.adminVerify(form.getToken(), form.getVerifyCode()));
    }

    @GetMapping("/search/{k}")
    @NeedAuth(User.A_ADMIN)
    public Response<ArrayWrapperVo<User>> getAllUsers(@PathVariable("k")String key) {
        return Response.ok(new ArrayWrapperVo<>(userService.searchUser(key)));
    }

    @PostMapping("/modifyPurchasedLevel")
    @NeedAuth(User.A_ADMIN)
    public Response modifyPurchased(@RequestBody User user) {
        userService.modifyUser(User.builder()
                .id(user.getId())
                .purchasedVipLevel(user.getPurchasedVipLevel()).build());
        return Response.ok();
    }

    @GetMapping("/{userId}/sendVip")
    @NeedAuth(User.A_ADMIN)
    public Response sendVip(@PathVariable("userId") String userId) {
        userService.earnVip(userService.getUser(userId), Integer.MAX_VALUE);
        return Response.ok();
    }
    @GetMapping("/{userId}/sendVip/{days}")
    @NeedAuth(User.A_ADMIN)
    public Response sendVip(@PathVariable("userId") String userId, @PathVariable("days")int days) {
        userService.earnVip(userService.getUser(userId), days);
        return Response.ok();
    }
}
